package com.ddone.demo.mapper;

import com.ddone.demo.bean.Emp;
import com.ddone.demo.utils.MybatisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author ddone
 * @date 2024/3/18-21:04
 */
@Slf4j
public class DynamicSqlTest {
    private SqlSession sqlSession;
    private EmpMapper mapper;

    @Before
    public void init() {
        sqlSession = MybatisUtil.openAutoSession();
        mapper = sqlSession.getMapper(EmpMapper.class);
    }

    @Test
    public void testIfAndWhere() {
        List<Emp> emps = mapper.queryEmpsIfAndWhere("李二狗", 300d);
        System.out.println("emps = " + emps);
    }

    @Test
    public void testSet() {
        mapper.updateEmpByIdSet(Emp.builder().empName("yyyyw").empId(6).empSalary(0d).build());
    }

    @After
    public void destroy() {
        sqlSession.close();
    }

    @Test
    public void testTrim() {
        List<Emp> emps = mapper.selectEmployeeByConditionByTrim(Emp.builder().empName("王大花").empSalary(10000d).build());
        System.out.println("emps = " + emps);
    }

    @Test
    public void testBatchQuery() {
        ArrayList<Integer> ids = new ArrayList<>(Arrays.asList(1,2,3,4,5,6, 7,9,10, 8));
        List<Emp> emps = mapper.batchQueryEmpByIds(ids);
        System.out.println("emps = " + emps);
    }

    @Test
    public void testBatchInsert() {
        ArrayList<Emp> emps = new ArrayList<Emp>() {{
            add(Emp.builder().empName("xxxyy").build());
            add(Emp.builder().empName("王铁柱").build());
            add(Emp.builder().empName("赵翠花").build());
        }};
        int rows = mapper.batchInsertEmp(emps);
        System.out.println("rows = " + rows);
    }

    @Test
    public void testBatchUpdated(){
        ArrayList<Emp> emps = new ArrayList<Emp>() {{
            add(Emp.builder().empName("xxxyy").empId(8).empSalary(20000d).build());
            add(Emp.builder().empName("王铁柱").empId(9).empSalary(1000d).build());
            add(Emp.builder().empName("赵翠花").empId(10).empSalary(3000d).build());
        }};
        int rows = mapper.batchUpdateEmp(emps);
        System.out.println("rows = " + rows);
    }

    @Test
    public void testSqlChip(){
        List<Emp> emps = mapper.queryEmpByParams(Emp.builder().empName(null).empSalary(10000d).build());
        System.out.println("emps = " + emps);
        log.info("测试中文是否乱码~~~");

    }
}
